package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.SelfHarvest;
import com.edward.model.SellList;
import com.edward.model.SellSum;


public class SellListManager extends HibernateDaoSupport {

	public void addSellList(SellList sellList) {
		
		getHibernateTemplate().save(sellList);
		addSellHarvest(sellList);
	}
	
	public void updateSellList(SellList sellList) {
		
		getHibernateTemplate().update(sellList);
		addSellHarvest(sellList);
	}
	
	public void addSellSum(SellSum sellSum) {
		
		getHibernateTemplate().save(sellSum);
	}
	
	public void updateSellSum(SellSum sellSum) {
		
		getHibernateTemplate().update(sellSum);
	}
	
	public void deleteSellList(int id){
		SellList s = new SellList(); 
		SellList sl = getSellList(id);
		s.setRunway(sl.getRunway());
		s.setDate(sl.getDate());
		s.setProject(sl.getProject());
		getHibernateTemplate().delete(sl);
		addSellHarvest(s);
	}
	
	public SellList getSellList(int id){
		SellList d =(SellList)getHibernateTemplate().get(SellList.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<SellList> listSellList() {
		return getHibernateTemplate().find("from SellList");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(new Date());
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
		String query1 = sdf1.format(new Date());
		String query2 = sdf2.format(new Date());
		List<SellList> list = getHibernateTemplate().find(
				"from SellList s where s.type = '" + type
						+ "' and s.runway = '批发' and s.date = '" + d
						+ "' order by s.company");
		SellList sl = new SellList();
		SellSum ss = new SellSum();
		sl.setSellSum(ss);
		sl.setRunway("小计");
		for (SellList st : list){
			sl.setMoney(st.getMoney() + sl.getMoney());
			sl.setCount(sl.getCount() + st.getCount());
		}
		List<SellList> l = getHibernateTemplate().find(
				"from SellList s where s.type = '" + type
						+ "' and s.runway = '批发"
						+ "' and year(s.date) = '" + query1
						+ "' and MONTH(s.date) = '" + query2 + "'");
		for (SellList st : l){
			sl.getSellSum().setTotal(st.getMoney() + sl.getSellSum().getTotal());
			sl.getSellSum().setAmount(st.getCount() + sl.getSellSum().getAmount());
		}
		list.add(sl);
		SellList sumS = new SellList();
		sumS.setId(99999);
		SellSum sumSS = new SellSum();
		sumSS.setId(99999);
		sumS.setSellSum(sumSS);
		sumS.setRunway("本月合计");
		List<SellList> suml = getHibernateTemplate().find(
				"from SellList s where s.type = '" + type
						+ "' and year(s.date) = '" + query1
						+ "' and MONTH(s.date) = '" + query2 + "'");
		for(SellList st : suml){
			sumS.getSellSum().setTotal(st.getMoney() + sumS.getSellSum().getTotal());
			sumS.getSellSum().setAmount(st.getCount() + sumS.getSellSum().getAmount());
		}
		list.add(sumS);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <SellList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,SellList s) {
		List <SellList>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from SellList s where s.type = '" + s.getType() +"'";
		if(!s.getCompany().equals("")){
	
			sql += "and s.company = '" + s.getCompany() + "'";
		}
		if(!s.getRunway().equals("")){
			
			sql += "and s.runway = '" + s.getRunway() + "'";
		}
		if(s.getDate() != null){
			String d = sdf.format(s.getDate());
			sql += "and s.date = '" + d + "'";
		}
		sql += "order by s.company";
		
		list = getHibernateTemplate().find(sql);
		
		
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
		String query1 = sdf1.format(new Date());
		String query2 = sdf2.format(new Date());
		SellList sl = new SellList();
		SellSum ss = new SellSum();
		sl.setSellSum(ss);
		
		sl.setRunway("小计");
		for (SellList st : list){
			sl.setMoney(st.getMoney() + sl.getMoney());
			sl.setCount(sl.getCount() + st.getCount());
		}
		List<SellList> l = getHibernateTemplate().find(
				"from SellList s where s.type = '" + s.getType()
						+ "' and s.runway = '" + s.getRunway()
						+ "' and year(s.date) = '" + query1
						+ "' and MONTH(s.date) = '" + query2 + "'");
		for (SellList st : l){
			sl.getSellSum().setTotal(st.getMoney() + sl.getSellSum().getTotal());
			sl.getSellSum().setAmount(st.getCount() + sl.getSellSum().getAmount());
		}
		list.add(sl);
		SellList sumS = new SellList();
		sumS.setId(999999);
		SellSum sumSS = new SellSum();
		sumSS.setId(999999);
		sumS.setSellSum(sumSS);
		sumS.setRunway("本月合计");
		List<SellList> suml = getHibernateTemplate().find(
				"from SellList s where s.type = '" + s.getType()
						+ "' and year(s.date) = '" + query1
						+ "' and MONTH(s.date) = '" + query2 + "'");
		for(SellList st : suml){
			sumS.getSellSum().setTotal(st.getMoney() + sumS.getSellSum().getTotal());
			sumS.getSellSum().setAmount(st.getCount() + sumS.getSellSum().getAmount());
		}
		list.add(sumS);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <SellList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public void addcheck(SellList sl){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdf2 = new SimpleDateFormat("MM");
		String query1 = sdf.format(sl.getDate());
		String query2 = sdf2.format(sl.getDate());
		String sql = "from SellList s where s.company = '" + sl.getCompany()
		+ "' and s.runway = '" + sl.getRunway()
		+ "' and year(s.date) = '" + query1
		+ "' and MONTH(s.date) = '" + query2
		+ "' and s.type = '"  + sl.getType()
		+ "' and SUM_ID is not null";
		
		List<SellList> list = getHibernateTemplate().find(sql);
		if(list.size()>0){
			SellList st = list.get(0);
			SellSum ss = st.getSellSum();
			ss.setAmount(ss.getAmount() + sl.getCount());
			ss.setTotal(ss.getTotal() + sl.getMoney());
			sl.setSellSum(ss);
			updateSellList(sl);
			updateSellSum(ss);
		}else{
			SellSum ss = new SellSum();
			ss.setAmount(sl.getCount());
			ss.setTotal(sl.getMoney());
			addSellSum(ss);
			sl.setSellSum(ss);
			updateSellList(sl);
		}
		
	}
	
	@SuppressWarnings("unchecked")
	public boolean addSellHarvest(SellList sellList){
		if(!sellList.getRunway().equals("批发")){
			return false;
		}
		
		SimpleDateFormat yy = new SimpleDateFormat("yyyy");
		SimpleDateFormat mm = new SimpleDateFormat("MM");
		String y = yy.format(sellList.getDate());
		String m = mm.format(sellList.getDate());
		double sum = 0, typeSum = 0;
		List<SellList> suml = getHibernateTemplate().find(
				"from SellList s where  year(s.date) = '" + y
						+ "' and MONTH(s.date) = '" + m + "'");
		for(SellList st : suml){
			sum += st.getMoney();
		}
		
		List<SellList> typeSuml = getHibernateTemplate().find(
				"from SellList s where s.project = '" + sellList.getProject()
						+ "' and year(s.date) = '" + y
						+ "' and MONTH(s.date) = '" + m + "'");
		for(SellList st : typeSuml){
			typeSum += st.getMoney();
		}
		
		
		List <SelfHarvest> list = getHibernateTemplate().find("from SelfHarvest s where year(s.date) = '" + y + "' and month(s.date) = '" + m + "'");
		if(list.size() != 0){
			for(SelfHarvest sh : list){
				if(sh.getType() == null){
					sh.setIncome(sum);
					sh.setGrossProfit(sh.getIncome()- sh.getCost());
					sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
					if(sh.getNetProfit()==0||sh.getIncome()==0){
						sh.setRate(0);
					}else{
						sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
						int i = 0;
						double d ;
						i = (int) (sh.getRate() * 1000);
						d = i;
						sh.setRate(d/1000);

						}
					 getHibernateTemplate().save(sh);
				}else{
					if(sh.getType().equals(sellList.getProject())){
						sh.setIncome(typeSum);
						sh.setGrossProfit(sh.getIncome()- sh.getCost());
						sh.setNetProfit(sh.getIncome() - sh.getCost() - sh.getFee() - sh.getTax() + sh.getBalance() - sh.getOther() - sh.getLoss());
						if(sh.getNetProfit()==0||sh.getIncome()==0){
							sh.setRate(0);
						}else{
							sh.setRate(sh.getNetProfit()/sh.getIncome()*100);
							int i = 0;
							double d ;
							i = (int) (sh.getRate() * 1000);
							d = i;
							sh.setRate(d/1000);
						}
						getHibernateTemplate().save(sh);
					}
				}
			}
		}else{
			SelfHarvest sh = new SelfHarvest();
			List <SelfHarvest> l = new ArrayList<SelfHarvest>();
			sh.setDate(sellList.getDate());
			sh.setIncome(sh.getIncome() + sellList.getMoney());
			sh.setSelfs(l);
			getHibernateTemplate().save(sh);
			for(int i=0; i<5;i++){
					SelfHarvest st = new SelfHarvest();
					st.setType(judge(i));
					st.setDate(sellList.getDate());
					if(judge(i).equals(sellList.getProject())){
						st.setIncome(sellList.getMoney());
					}
					getHibernateTemplate().save(st);
					sh.getSelfs().add(st);
			}
			getHibernateTemplate().save(sh);
		}
		
		return true;
	}
	
	
	public String judge (int i){
		if(i == 0){
			return "汽车";
		}else if (i == 1){
			return "摩托车";
		}else if (i == 2){
			return "汽车配件";
		}else if (i == 3){
			return "摩托车配件";
		}else{
			return "其他";
		}
	}
	


}



